package s10;

import base.Bean2;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * BeanMap
 *
 * @author wangpp
 */
public class BeanMapTest {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        BeanGenerator generator = new BeanGenerator();
        generator.addProperty("username", String.class);
        generator.addProperty("password", String.class);


        Object bean = generator.create();
        Method getUsername = bean.getClass().getMethod("getUsername");
        Method setUsername = bean.getClass().getMethod("setUsername", String.class);
        Method getPassword = bean.getClass().getMethod("getPassword");
        Method setPassword = bean.getClass().getMethod("setPassword", String.class);


        setUsername.invoke(bean, "admin");
        setPassword.invoke(bean, "123456");

        BeanMap beanMap = BeanMap.create(bean);

        System.out.println(beanMap.get("username"));
        System.out.println(beanMap.get("password"));

        System.out.println(getUsername.invoke(bean));
        System.out.println(getPassword.invoke(bean));

        //
        Bean2 bean2 = new Bean2();
        bean2.setValue("hello");

        BeanMap map = BeanMap.create(bean2);
        System.out.println(map.get("value"));
    }
}
